// 下拉加载更多
export default {
  bind(el, binding) {
    const { arg, value, modifiers } = binding
    const distance = +Object.keys(modifiers)?.[0] || 0
    const selectWrap = el.querySelector(`.${arg}`) // 滚动的容器
    const handler = function () {
      const isNext = Math.abs(this.scrollHeight - this.scrollTop - this.clientHeight) < 1 + distance
      if (isNext) {
        value()
      }
    }
    selectWrap.addEventListener("scroll", handler)
    el.__handler__ = handler
  },
  unbind(el, binding) {
    const selectWrap = el.querySelector(`.${binding.arg}`)
    if (selectWrap && el.__handler__) {
      selectWrap.removeEventListener("scroll", el.__handler__)
    }
  },
}
